package com.coolingme.general.transparent;

/**
 * 组合模式的透明写法测试
 *
 * @author wangyue
 * @date 2020-09-18 23:44
 */
public class Test {

    public static void main(String[] args) {
        // 根节点
        Composite root = new Composite("root");
        // 树枝节点
        Composite branchA = new Composite("----branchA");
        Composite branchB = new Composite("----branchB");
        // 叶子结点
        Leaf leafA = new Leaf("--------leafA");
        Leaf leafB = new Leaf("--------leafB");
        Leaf leafC = new Leaf("--------leafC");

        branchA.addChild(leafA);
        branchA.addChild(leafB);
        branchB.addChild(leafC);

        root.addChild(branchA);
        root.addChild(branchB);

        // 组合结构
        String result = root.operation();
        System.out.println(result);

        System.out.println("\n---------------");

        // 删除节点
        branchB.removeChild(leafC);
        result = root.operation();
        System.out.println(result);

        System.out.println("\n---------------");

        // 获取节点
        Component rootChild = root.getChild(0);
        result = rootChild.operation();
        System.out.println(result);

    }

}
